<template>
    <div>

    </div>
</template>

<script>
    import {auth} from "../libs/wechat";
    import {mapActions} from "vuex";
    import localStorage from "../utils/store/localStorage";
    import {registerActivityLead} from "../api/user";
    import {Toast} from "vant";

    export default {
        name: "WechatLogin",
        data() {
            return {}
        },
        mounted() {
            const {code} = this.$route.query;
            auth(code)
                .then(userId => {
                    this.USERINFO(userId).then(data => {
                        localStorage.set("user", data);
                        const login_back_url = localStorage.get("login_back_url");
                        const smId = localStorage.get('smId')
                        registerActivityLead(smId,userId).then(res => {
                            if (res.code) {
                                if (login_back_url) {
                                    this.$router.push(login_back_url)
                                } else {
                                    this.$router.push('/')
                                }
                            }else {
                                // return false
                                Toast('非法用户注册！');
                                this.$router.push('/')
                            }
                        })
                    });
                })
                .catch(() => {
                    console.log('catch');
                    // location.href = '/bindingMobile'
                    this.$router.push('bindingMobile')
                })
        },
        methods: {
            ...mapActions(['USERINFO'])
        }
    }
</script>

<style scoped>

</style>
